package com.agy.filemanager.common;


import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

/**
 * HDFS文档操作类
 */
@Component
public class HDFSUtil {

//    @Autowired
//    private FileSystem fileSystem;
//
//    /**
//     * 上传文件
//     * @param path
//     * @param inputFile
//     * @return
//     */
//    public boolean uploadFile(String path , InputStream inputFile) throws IOException {
//        FSDataOutputStream out = fileSystem.create(new Path(path), true);
//        IOUtils.copyBytes(inputFile,out,4096,true);
//        return  true;
//    }
//
//    /**
//     * 文档下载
//     * @param path
//     * @return
//     * @throws IOException
//     */
//    public InputStream downloadFile(String path) throws IOException {
//        FSDataInputStream outPut = fileSystem.open(new Path(path));
//        return  outPut;
//    }
//
//    public static void main(String[] args) throws Exception{
//        Configuration conf = new Configuration();
//        FileSystem fs = FileSystem.get(new URI("hdfs://10.206.1.82:9000/"), conf);
//        FSDataOutputStream fsOut = fs.create(new Path("/filemanager/test/test.txt"),true);
//        FileInputStream fsIn = new FileInputStream("D:\\bi.txt");
//        IOUtils.copyBytes(fsIn,fsOut,4096,true);
//    }
    @Autowired
    private FileSystem fileSystem;

    /**
     * 上传文件
     * @param path
     * @param inputFile
     * @return
     */
    public boolean uploadFile(String path , InputStream inputFile) throws IOException {
        FSDataOutputStream out = fileSystem.create(new Path(path), true);
        IOUtils.copyBytes(inputFile,out,4096,true);
        return  true;
    }

    /**
     * 文档下载
     * @param path
     * @return
     * @throws IOException
     */
    public InputStream downloadFile(String path) throws IOException {
        FSDataInputStream outPut = fileSystem.open(new Path(path));
        return  outPut;
    }

   /* public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://10.206.1.82:9000/"), conf);
        FSDataOutputStream fsOut = fs.create(new Path("/filemanager/test/test.txt"),true);
        FileInputStream fsIn = new FileInputStream("D:\\bi.txt");
        IOUtils.copyBytes(fsIn,fsOut,4096,true);
    }*/
}
